package com.example.hello;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;


@SuppressLint("NewApi") 
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private ActionBar actionBar;
	// Tab titles
	private String[] tabs = { "Peak Detect", "Line Follower"};
	
	
	
	 
	 
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		// Initilization
		viewPager = (ViewPager) findViewById(R.id.view_pager);
		actionBar = getActionBar();
		mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
		
		viewPager.setAdapter(mAdapter);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);       
		
		// Adding Tabs
		for (String tab_name : tabs) {
		    actionBar.addTab(actionBar.newTab().setText(tab_name)
		            .setTabListener(this));
		}
		
		
		

	/**
	 * on swiping the viewpager make respective tab selected
	 * */
	viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
	
	    @Override
	    public void onPageSelected(int position) {
	        // on changing the page
	        // make respected tab selected
	        actionBar.setSelectedNavigationItem(position);
	    }
	
	    @Override
	    public void onPageScrolled(int arg0, float arg1, int arg2) {
	    }
	
	    @Override
	    public void onPageScrollStateChanged(int arg0) {
	    }
	});
	}


	
	public class TabsPagerAdapter extends FragmentPagerAdapter {
		 
	    public TabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
	        super(fm);
	    }
	 
	    @Override
	    public Fragment getItem(int index) {
	 
	        switch (index) {
	        case 0:
	            // Top Rated fragment activity
	            return new PeakDetectActivity();
	        case 1:
	            // Games fragment activity
	            return new PeakDetectActivity();
	       
	        }
	 
	        return null;
	    }
	 
	    @Override
	    public int getCount() {
	        // get item count - equal to number of tabs
	        return 2;
	    }
	 
	}
	
	@Override
	public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		// on tab selected
		// show respected fragment view
		
		if(tab.getPosition()==0){
			 
		} else if(tab.getPosition()==1){
			
			
		}
		viewPager.setCurrentItem(tab.getPosition());
	}
	
	@Override
	public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	public int MENU_DELETE_CONNECT = 0;
	public int MENU_EXIT = 1;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, MENU_DELETE_CONNECT, 0, "Break the connection");
		menu.add(0, MENU_EXIT, 1, "Back to the previous page");
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==MENU_DELETE_CONNECT){
			
		}else if(item.getItemId()==MENU_EXIT){
				
		}
		
		return super.onOptionsItemSelected(item);
	}


}

